package logic

import (
	"ball/ballrpc/ballrpc"
	"ball/ballrpc/internal/svc"
	"ball/cache"
	"ball/models/mysql"
	"context"
	"fmt"
	"github.com/zeromicro/go-zero/core/logx"
)

type UserAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserAddLogic {
	return &UserAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 注册
func (l *UserAddLogic) UserAdd(in *ballrpc.UserAddRequest) (*ballrpc.UserAddResponse, error) {
	// todo: add your logic here and delete this line
	user := mysql.User{
		Account:  in.Account,
		Password: in.Password,
		Address:  in.Address,
	}
	mysql.DB.Debug().Where("account=?", in.Account).Find(&user)
	get := cache.Rdb.Get(l.ctx, "SendSms"+in.Tel)
	if get.Val() != in.SendSms {
		return nil, fmt.Errorf("短信验证码错误")
	}
	if user.ID != 0 {
		return nil, fmt.Errorf("一个手机号只能注册一次")
	}
	mysql.DB.Debug().Create(&user)
	return &ballrpc.UserAddResponse{
		Id: int64(user.ID),
	}, nil
}
